<?php

namespace app\api\controller\develop;
use app\api\service\SchemaService;
use app\BaseController;
use app\api\service\develop\TableService;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\api\service\log\AdminOperateLogService;
use app\util\AdminUserUtil;
use app\util\ResponseUtil;
use think\App;

class Table extends BaseController
{

    private $pageCode = 'page_toolbar';
    private $arrFieldTitle = array("status" => "状态");

    public function __construct(App $app)
    {
        parent::__construct($app);
        $servicePageColumn = new DatagridColumnService();
        $list = $servicePageColumn->getListNoPage(array('page_code' => $this->pageCode, 'status' => 1));
        $this->arrFieldTitle = array_column($list, 'column_title', 'column_name');
    }

    public function getTableInfo()
    {
        $id = $this->requestData['id'];
        $service = new TableService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getTableList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new TableService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getTableOption()
    {
        $service = new TableService();
        $list = $service->getListNoPage($this->requestData, 100);
        return ResponseUtil::success($list);
    }

    public function addTable()
    {
        try{
            if(empty($this->requestData['page_code'])){
                return ResponseUtil::failure("页面标识码不可为空");
            }
            $pageCode = $this->requestData['page_code'];

            $servicePageColumn = new DatagridColumnService();
            $columnList = $servicePageColumn->getListNoPage(['page_code' => $pageCode, 'is_in_add'=>1, 'is_require' => 1]);
            foreach($columnList as $columnItem){
                if(empty($this->requestData[$columnItem['column_name']])){
                    return ResponseUtil::failure($columnItem['column_title']."不可为空");
                }
            }

            $service = new TableService();
            $res = $service->createRow($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $this->arrFieldTitle);
                $message = '新增库表';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Table', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增库表失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增库表失败");
        }
    }

    public function editTable()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的库表");
            }

            $id = $this->requestData['id'];
            $service = new TableService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑库表,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Table', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑库表失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑库表失败");
        }
    }

    public function deleteTable()
    {
        try{
            $id = $this->requestData['id'];
            $service = new TableService();
            $info = $service->findRowById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->removeRowById($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($info, $this->arrFieldTitle);
                $message = '删除库表';
                if($deleteInfo){
                    $message .= ','.$deleteInfo;
                }
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Table', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除库表失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除库表失败");
        }
    }

    public function deleteMultiTable(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new TableService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->findRowById($value);
                if(!$info){
                    return ResponseUtil::failure("库表不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->removeListByManyId($id);
            if($res){
                $message = '删除库表';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Table', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除库表失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除库表失败");
        }
    }

    public function simpleUpdateTable()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的库表");
            }

            $id = $this->requestData['id'];
            $service = new TableService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑库表,'.$changeInfo['update_info'];
                $logData = [
                    'subject' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('Table', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑库表失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑库表失败");
        }
    }

    /**
     * Desc: 同步库表数据
     * Datetime: 2024/11/28 17:15 by zdb
     */
    public function synTable()
    {
        try{
            $tableService = new TableService();
            $res = $tableService->synchronizeTable();
            if(1 == $res['flag']){
                return ResponseUtil::success("同步库表成功");
            }else{
                return ResponseUtil::failure($res['message']);
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("同步库表失败");
        }
    }
}



